home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / VBASIC / FINDFILE.ZIP / FINDFILE.BAS
BASIC Source File  |  1996-03-10  |  2KB  |  86 lines

  1. Option Explicit
  2.  
  3. Const ATTR_DIRECTORY = 16
  4.  
  5. Function FindFile$ (srcPath$, Filename$)
  6.  
  7. ' Description
  8. '     Finds a file in the subdirs under srcPath
  9. '
  10. ' Parameters
  11. '     Name                 Type     Value
  12. '     ------------------------------------------------------------
  13. '     srcPath              String   The path so start searching in
  14. '     Filename             String   The file to look for
  15. '
  16. ' Returns
  17. '     The first occurence of Filename$
  18. '
  19. ' Last modified by Jens Balchen 10.03.1996
  20.  
  21. Dim DirReturn As String, i As Integer
  22. Dim subdirs$
  23. Dim filefound$
  24. Dim path$
  25.  
  26.    ' If Path lacks a "\", add one to the end
  27.    If Right$(srcPath, 1) <> "\" Then srcPath = srcPath & "\"
  28.    srcPath = UCase$(srcPath)
  29.    Filename$ = UCase$(Filename$)
  30.    
  31.    ' Now find if the file is here
  32.    DirReturn = Dir(srcPath & Filename, 0)
  33.  
  34.    ' If it is, return the filename and dir
  35.    If DirReturn$ <> "" Then
  36.       FindFile$ = srcPath$ & Filename$
  37.       Exit Function
  38.    End If
  39.  
  40.    ' No, it wasn't here. Do all the subdirs
  41.    ' Initialize var to hold filenames
  42.    DirReturn = Dir(srcPath & "*.*", ATTR_DIRECTORY)
  43.    
  44.    ' Find all subdirs
  45.    Do While DirReturn <> ""
  46.       ' Make sure we don't do anything with "." and "..", they aren't real files
  47.       If DirReturn <> "." And DirReturn <> ".." Then
  48.          DoEvents
  49.          If GetAttr(srcPath & DirReturn) = ATTR_DIRECTORY Then
  50.             ' It's a dir. Add it to dirlist
  51.             subdirs$ = subdirs$ & srcPath & DirReturn & ";"
  52.          End If
  53.       End If
  54.       DirReturn = Dir
  55.    Loop
  56.  
  57.    ' Do all subs
  58.    Do
  59.       
  60.       i% = i% + 1
  61.       If Mid$(subdirs$, i%, 1) = ";" Then
  62.          path$ = Left$(subdirs$, i% - 1)
  63.          If path$ <> "" Then
  64.             filefound$ = FindFile$(path$, Filename$)
  65.             If filefound$ <> "" Then
  66.                FindFile$ = filefound$
  67.                Exit Function
  68.             End If
  69.             subdirs$ = Right$(subdirs$, Len(subdirs$) - i%)
  70.             i% = 0
  71.          End If
  72.       End If
  73.  
  74.       DoEvents
  75.  
  76.       If subdirs$ = "" Then Exit Do
  77.  
  78.    Loop
  79.  
  80. ExitFunc:
  81.  
  82.    Exit Function
  83.  
  84. End Function
  85.  
  86.